package com.njupt.DynamicProgramming;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/2/19 21:23
 * @Description: 198. 打家劫舍
 * @Version: 1.0
 */


public class Rob_198 {

    /**
     * 投机取巧
     * @param nums
     * @return
     */
    public int rob1(int[] nums) {
        if(nums.length == 1){
            return nums[0];
        } else if (nums.length == 2) {
            return Math.max(nums[0],nums[1]);
        }

        int sum1 = 0;
        for (int i = 0; i < nums.length; i += 2) {
            sum1 += nums[i];
        }

        int sum2 = 0;
        for (int i = 1; i < nums.length; i += 2) {
            sum2 += nums[i];
        }

        return sum1 > sum2? sum1 : sum2;
    }


    /**
     * 动态规划
     * @param nums
     * @return
     */
    public int rob(int[] nums) {
        if(nums.length == 1){
            return nums[0];
        }else if(nums.length == 2){
            return Math.max(nums[0],nums[1]);
        }
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        dp[1] = Math.max(nums[0],nums[1]);
        for (int i = 2; i < dp.length; i++) {
            dp[i] =Math.max(dp[i-1],dp[i-2] + nums[i]);
        }
        return dp[dp.length-1];

    }

    public static void main(String[] args) {
        int nums[] = {1,2,3,1};
        Rob_198 test = new Rob_198();
        System.out.println(test.rob(nums));
    }
}
